package org.morning.open.connectionPool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

public class JdbcConnectionBuilder implements ConnectionBuilder<Connection> {
	private String dbUrl;
	private String username;
	private String password;

	public JdbcConnectionBuilder(Map<String, Object> args) {
		String driverClassName = (String) args.get("driverClassName");
		try {
			Class.forName(driverClassName);
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dbUrl = (String) args.get("dbUrl");
		username = (String) args.get("username");
		password = (String) args.get("password");
	}

	@Override
	public Connection create(String host, int port, Map<String, Object> args) {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(dbUrl, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}

	@Override
	public void close(Connection connection) {
		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
